﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SAM_Edu.control;
using SAM_Edu.entity;
using System.IO;
using SAM_Edu.Services;

namespace SAM_Edu.form
{
    public partial class frmSetupSW : Form
    {
        SetupSoftware ss = new SetupSoftware();
        public frmSetupSW()
        {
            InitializeComponent();
        }

        private void tbLicenseKeyPart1_MouseClick(object sender, MouseEventArgs e)
        {
            if (tbLicenseKeyPart1.Text == "xxxx")
            {
                tbLicenseKeyPart1.Text = "";
            }
        }

        private void tbLicenseKeyPart2_MouseClick(object sender, MouseEventArgs e)
        {
            if (tbLicenseKeyPart2.Text == "xxxx")
            {
                tbLicenseKeyPart2.Text = "";
            }
        }

        private void tbLicenseKeyPart3_MouseClick(object sender, MouseEventArgs e)
        {
            if (tbLicenseKeyPart3.Text == "xxxx")
            {
                tbLicenseKeyPart3.Text = "";
            }
        }

        private void tbLicenseKeyPart4_MouseClick(object sender, MouseEventArgs e)
        {
            if (tbLicenseKeyPart4.Text == "xxxx")
            {
                tbLicenseKeyPart4.Text = "";
            }
        }

        private void tbLicenseKeyPart2_TextChanged(object sender, EventArgs e)
        {
 
            if (tbLicenseKeyPart2.Text != "xxxx" && tbLicenseKeyPart2.TextLength == 4)
            {
                tbLicenseKeyPart3.Focus();
            }
        }

        private void tbLicenseKeyPart3_TextChanged(object sender, EventArgs e)
        {
            if (tbLicenseKeyPart3.Text != "xxxx" && tbLicenseKeyPart3.TextLength == 4)
            {
                tbLicenseKeyPart4.Focus();
            }

        }

        private void tbLicenseKeyPart4_TextChanged(object sender, EventArgs e)
        {


        }
        public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
        private void btnRegister_Click(object sender, EventArgs e)
        {
            //byte[] imagebytes = ImageToByteArray(Image.FromFile(openFileDialog1.FileName));
            //byteArrayToImage(imagebytes);

            //SimpleService s = new SimpleService();
            //license[] L = new license[100];
            //L = s.getLicense();


            //Call webservice to check license
            //for mock up we say validated
            if (tbLicenseKeyPart1.Text == "xxxx" && tbLicenseKeyPart2.Text == "xxxx" && tbLicenseKeyPart3.Text == "xxxx" && tbLicenseKeyPart4.Text == "xxxx")
            {
                MessageBox.Show("Please insert License Key!");
            }
            else if (tbLicenseKeyPart1.Text.Count() != 4 && tbLicenseKeyPart2.Text.Count() != 4 && tbLicenseKeyPart3.Text.Count() != 4 && tbLicenseKeyPart4.Text.Count() != 4)
            {
                MessageBox.Show("Please insert License Key!");
            }
            else if (tbLisenceeName.Text == "" && tbLicenseeAddress.Text == "")
            {
                MessageBox.Show("Please insert Licensee Name & Address!");
            }
            else if (tbLicenseeAddress.Text == "")
            {
                MessageBox.Show("Please insert Licensee Name!");
            }
            else if (tbLisenceeName.Text == "")
            {
                MessageBox.Show("Please insert Licensee Name!");
            }
            else if (tbLogoLink.Text == "")
            {
                MessageBox.Show("Please select Logo Image!");
            }
            else
            {
                string licensekey = tbLicenseKeyPart1.Text + tbLicenseKeyPart2.Text + tbLicenseKeyPart3.Text + tbLicenseKeyPart4.Text;
                RegisterLicense rl = new RegisterLicense();
                string results = rl.retrieveLicense(licensekey,tbLisenceeName.Text);
                //string results = "true";
                if (results == "true")
                {

                    //Insert license key into database
                    
                    SetupSoftware.insertLicenseKey(licensekey, 1, DateTime.Now, DateTime.Now, 1, 0);

                    //Insert License Details into Database
                    string licenseeName = tbLisenceeName.Text;
                    string licenseeAddess = tbLicenseeAddress.Text;
                    byte[] imagebyte = ImageToByteArray(Image.FromFile(openFileDialog1.FileName));
                    SetupSoftware.insertLicense(licenseeName, licenseeAddess, imagebyte);

                    SAM_Edu.entity.License l = Startup.getLicensee();
                    this.ParentForm.Controls["lbLicense"].Text = ("xxxx-xxxx-xxxx-" + licensekey.Substring(12, licensekey.Length - 12));
                    this.ParentForm.Controls["lblFooter"].Text = "SAM Teaching Material \n" + "Licensed To " + l.Name.ToString() + "\nCopyright 2014 EightyTwenty PTE LTD";
                    this.ParentForm.Controls["companylogo"].BackgroundImage = byteArrayToImage(l.Logo);
                    // Database should preload with admin account
                    frmLogon sw = new frmLogon();
                    sw.MdiParent = this.ParentForm;
                    sw.StartPosition = FormStartPosition.Manual;
                    sw.Location = new System.Drawing.Point(10, 10);
                    sw.Show();
                    this.Hide();
                }
                else
                {
                    // Update thhe number of warnings
                }
            }
        }

        public byte[] ImageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();

            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();

        }

        private void tbLicenseKeyPart1_TextChanged(object sender, EventArgs e)
        {

            if (tbLicenseKeyPart1.Text != "xxxx" && tbLicenseKeyPart1.TextLength == 4)
            {
                tbLicenseKeyPart2.Focus();
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbLogoLink.Text = openFileDialog1.FileName;
                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);

            }
        }

        private void frmSetupSW_Load(object sender, EventArgs e)
        {

        }
    }
}
